<template>
  <div class="userrole">
    <el-dialog
      :title="state ? '编辑用户角色' : '添加角色'"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        :model="submitData"
        status-icon
        :label-position="labelPosition"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <div class="userinfo">
          <div class="userinfo-left">
            <div class="avatar-wrap">
              <img
                style="
                  display: block;
                  width: 92px;
                  height: 92px;
                  border-radius: 10px;
                "
                v-if="submitData.avatar"
                :src="
                  submitData.avatar ===
                  'https://i01piccdn.sogoucdn.com/188479196024fd76'
                    ? submitData.avatar
                    : `${this.$upload_path}/${submitData.avatar}`
                "
              />
            </div>
          </div>
          <div class="userinfo-right">
            <div class="nickName-wrap">
              <span>{{ submitData.nickName }}</span>
            </div>
            <div class="role-wrap">
              <span>{{ submitData.role ? submitData.role : '暂无角色' }}</span>
            </div>
          </div>
        </div>
        <div class="flex-col-8">
          <el-form-item label="选择角色">
            <el-select v-model="submitData.roleId" placeholder="请选择">
              <el-option
                v-for="item in choices"
                :key="item.id"
                :label="item.role"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="flex-col-8">
          <el-form-item label="选择状态">
            <el-select v-model="submitData.status" placeholder="状态">
                <el-option label="已取消授权" :value="0"></el-option>
                <el-option label="已授权" :value="1"></el-option>
                <el-option label="审核中" :value="2"></el-option>
              </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="grantRole"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <div class="inner-container container">
      <div class="filter-container flex-row">
        <div class="flex-col-3 input-box">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入昵称"
          ></el-input>
        </div>
        <div class="flex-col-3 input-box">
          <el-select v-model="queryParams.role" placeholder="请选择角色">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.role"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="btn-box">
          <el-button type="primary" icon="el-icon-search" plain @click="search" v-permission="{ action: 'view', effect: 'disabled' }"
            >搜索</el-button
          >
          <el-button type="danger" icon="el-icon-delete" plain @click="batchDelete" v-permission="{ action: 'delete', effect: 'disabled' }"
            >批量删除</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableData"
        border
        v-loading="loading"
        @selection-change="handleSelectionChange"
        style="width: 100%"
        :header-cell-style="{
          background: 'var(--primary-color)',
          color: 'var(--text-sidebar-hover-color)',
        }"
      >
        <el-table-column align="center" type="selection" width="66">
        </el-table-column>
        <el-table-column align="center" type="index" label="ID" width="66">
        </el-table-column>
        <el-table-column align="center" label="头像" width="66">
          <template slot-scope="scope">
            <img
              v-if="scope.row.avatar"
              style="width: 100%; border-radius: 10px"
              :src="
                scope.row.avatar ==
                'https://i01piccdn.sogoucdn.com/188479196024fd76'
                  ? scope.row.avatar
                  : `${$upload_path}/${scope.row.avatar}`
              "
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="nickName" label="昵称">
        </el-table-column>
        <el-table-column align="center" prop="role" label="角色">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" prop="createUser" label="创建者">
        </el-table-column>
        <el-table-column align="center" prop="updateTime" label="修改时间">
        </el-table-column>
        <el-table-column align="center" prop="updateUser" label="操作者">
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status === 0"
              >取消授权</el-tag
            >
            <el-tag type="success" v-if="scope.row.status === 1"
              >已授权</el-tag
            >
            <el-tag type="warning" v-if="scope.row.status === 2"
              >待审核</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              plain
              type="primary"
              class="edit"
              icon="el-icon-edit"
              v-permission="{ action: 'update', effect: 'disabled' }"
              @click="editData(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDeleteRow(scope.row.userId)"
            >
              <el-button plain type="danger" slot="reference" icon="el-icon-delete" v-permission="{ action: 'delete', effect: 'disabled' }">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
        <template slot="empty">
          <el-empty :image-size="200" description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { userRolePageQuery, grantRole, deleteUserRole } from '@/api/user'
import { roleOptions, roleEffectiveOptions } from '@/api/role'
import { getTableData, handleVoidMethod } from '@/api/table'
export default {
  data() {
    return {
      options: [
        {
          id: -1,
          role: '全部',
        },
      ],
      choices: [],
      tableData: [],
      multipleSelection: [],
      queryParams: {
        role: '',
        nickName: '',
        page: 1,
        pageSize: 10,
      },
      submitData: {
        roleId: '',
        userId: '',
      },
      total: 0,
      state: false,
      loading: false,
      currentPage: 1,
      labelPosition: 'left',
      dialogFormVisible: false,
    }
  },
  created() {
    this.getTableData()
    this.getRoleOptions()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryParams.page = val
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = []
      val.forEach(item => {
        const { userId } = item
        this.multipleSelection.push(userId)
      })
    },
    handleDeleteRow(id) {
      console.log(id)
      deleteUserRole([id]).then((res) => {
        if (res.code === 200) {
          this.$message({ type: 'success', message: '删除成功' })
          this.getTableData()
        } else {
          this.$message({ type: 'success', message: '删除失败' })
        }
      })
    },
    cancel() {
      this.submitData = {}
      this.dialogFormVisible = false
    },
    getTableData() {
      this.loading = true
      getTableData(this, userRolePageQuery(this.queryParams))
    },
    async getRoleOptions() {
      const { data: res } = await roleOptions()
      this.options = [...this.options, ...res]
      await roleEffectiveOptions().then(res => {
        this.choices = res.data
      })
    },
    search() {
      this.getTableData()
    },
    editData(item) {
      this.state = true
      this.submitData = JSON.parse(JSON.stringify(item))
      this.dialogFormVisible = true
    },
    async grantRole() {
      handleVoidMethod(this, '修改成功', '修改失败', grantRole(this.submitData), this.cancel)
    },
    batchDelete() {
      if(this.multipleSelection.length <= 0) {
        return this.$message({ type: 'warning', message: '请选择对象' })
      }
      handleVoidMethod(this, '删除成功', '删除失败', deleteUserRole(this.multipleSelection))
    }
  },
}
</script>

<style lang="less" scoped>
.userrole {
  padding: 10px;
  box-sizing: border-box;
  .filter-container {
    flex-wrap: wrap;
    box-sizing: border-box;
    margin-bottom: 20px;
  }
  .inner-container {
    box-sizing: border-box;
    border-radius: 5px;
    padding: 20px;
    background-color: var(--bg-primary-color);
    transition: var(--tran-01);
  }
  .input-box {
    margin-right: 10px;
  }
}
.el-select {
  width: 100%;
}
/deep/ .el-dialog {
  background: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-dialog__title {
  color: var(--text-normal-color) !important;
  transition: var(--tran-01);
}
/deep/ .el-form-item__label {
  color: var(--text-normal-color) !important;
  transition: var(--tran-01);
}
.el-form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.userinfo {
  display: flex;
  margin-bottom: 20px;
  color: var(--text-normal-color);
  transition: var(--tran-01);
  .userinfo-left {
    margin-right: 20px;
  }
  .userinfo-right {
    // display: flex;
    // flex-direction: column;
    // justify-content: space-evenly;
    margin-right: 20px;
    .nickName-wrap {
      font-size: 24px;
      line-height: 28px;
      margin-bottom: 20px;
    }
    .role-wrap {
      font-size: 16px;
      line-height: 20px;
    }
  }
}
// /deep/ .el-form-item__label {
//   text-align: center !important;
// }
.el-pagination {
  margin-top: 20px;
}

/deep/ .el-pagination button {
  background-color: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-pager li {
  background-color: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-pager li.active {
  color: var(--primary-color);
  background-color: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-pagination__total {
  color: var(--text-normal-color);
  transition: var(--tran-01);
}
/deep/ .el-pagination__jump {
  color: var(--text-normal-color);
  transition: var(--tran-01);
}
.btn-box {
  display: flex;
  flex-wrap: wrap;
}
/deep/ .el-button {
  flex-grow: 1;
}
/deep/ .el-input .el-input__inner {
  color: var(--text-normal-color);
  background-color: var(--history-color) !important;
  transition: var(--tran-01);
}

/deep/ .el-select-dropdown {
  background-color: var(--history-color) !important;
}
/deep/ .el-table,
.el-table__expanded-cell {
  background-color: transparent;
}

/deep/ .el-table__body-wrapper.is-scrolling-left::-webkit-scrollbar,
/deep/ .el-table__body-wrapper.is-scrolling-right::-webkit-scrollbar,
/deep/ .el-table__body-wrapper.is-scrolling-middle::-webkit-scrollbar {
  height: 5px;
}
/deep/ .el-table__body-wrapper.is-scrolling-left::-webkit-scrollbar-thumb,
/deep/ .el-table__body-wrapper.is-scrolling-right::-webkit-scrollbar-thumb,
/deep/ .el-table__body-wrapper.is-scrolling-middle::-webkit-scrollbar-thumb {
  background: #ccc; // 滑块颜色
  border-radius: 6px; // 滑块圆角
}
/deep/ .el-table__body-wrapper.is-scrolling-left::-webkit-scrollbar-track,
/deep/ .el-table__body-wrapper.is-scrolling-right::-webkit-scrollbar-track,
/deep/ .el-table__body-wrapper.is-scrolling-middle::-webkit-scrollbar-track {
  border-radius: 10px; // 轨道圆角
  background-color: #fff // 轨道颜色 ;
}
/deep/ .el-table tr {
  color: var(--text-normal-color);
  background-color: var(--history-color);
  transition: var(--tran-01);
}

/deep/ .el-table__body tr:hover > td {
  background-color: #a69ff5 !important;
  color: var(--text-sidebar-hover-color);
}

// /deep/ .el-table .cell {
//   display: flex;
//   align-items: center;
//   justify-content: center;
// }

.edit {
  margin-right: 10px;
}
</style>
